$(function(){
    var $goodsPopup = $("#goodsPopup");
    var $numberIpt = $goodsPopup.find(".js-number-input");
    var $selectBtn = $("#selectBtn");
    var $selectedName = $("#selectedName");
    var $mask = $(".weui-popup-overlay");
    var $tapId = $("#tapId");

    //商品轮播图
    (function($){
        var len = $(".swiper-wrapper").find("img").length;
        var propertyItems = $goodsPopup.find(".js-property-item");

        $("#sumNum").html(len);
        $(".swiper-container").swiper({
            onSlideChangeEnd: function(swiper) {
                $("#currentNum").html(swiper.activeIndex + 1);
            }
        });

        if(propertyItems.length == 1){
            propertyItems.addClass("on");
            PropertySelect(propertyItems);
        }
    }(Zepto));

    $(document).on("click", "#goodsPopup", function(e){
        var $target = $(e.target);

        if($target.hasClass("js-property-item")){
            PropertySelect($target);
        }
        else if($target.hasClass("js-number-sub")){
            NumberSub($target);
        }
        else if($target.hasClass("js-number-plus")){
            NumberPlus($target);
        }
        else if($target.hasClass("js-complete-btn")){
            CompleteSelect();
        }
    });

    $numberIpt.on("input propertychange", function(){
        var $this = $(this);

        OptionIpt($this);
    });

    $selectBtn.on("click", function(){
        $tapId.val(0);
    });

    $mask.on("click", function(){
        $.closePopup("#goodsSelector");
    });

    $(document).on("click", "#optionNavbar", function(e){
        var $target = $(e.target);
        var name = $selectedName.text();

        if($target.hasClass("js-join-cart")){
            $tapId.val(1);
            if(name == "请选择"){
                $target.addClass("open-popup");
                return ;
            }
            else{
                intoCart();
            }
        }
        else if($target.hasClass("js-buy-btn")){
            $tapId.val(2);
            if(name == "请选择"){
                $target.addClass("open-popup");
                return ;
            }
            else{
                $target.removeClass("open-popup");
                orderConfirm();
            }
        }
    });
});

//选择商品材质
function PropertySelect(item){
	/*$(".goods_summary__img img.js-item-img").one("error", function(e){
         $(this).attr("src", $("#goodsImage").val());
    });*/
	
    var $goodsPopup = item.closest("#goodsPopup");
    var name = item.attr("data-name");
    var image = item.attr("data-image");
    var price = item.attr("data-discntPrice");
	
    item.addClass("on").siblings().removeClass("on");
    $goodsPopup.find(".js-item-img").attr("src", image);
    $goodsPopup.find(".js-item-name").text(name);
    $goodsPopup.find(".js-item-price").text(price);
}

//数量减
function NumberSub(element) {
    var $parent = element.closest(".js-numberbox");
    var $ipt = $parent.find(".js-number-input");
    var iptValue = parseInt($ipt.val());

    if(!isNaN(iptValue)){
        iptValue--;
        if(iptValue <= 1){
            iptValue = 1;
            element.addClass("mallui_number-disabled");
        }
        $ipt.val(iptValue);
    }
}

//数量加
function NumberPlus(element) {
    var $parent = element.closest(".js-numberbox");
    var $ipt = $parent.find(".js-number-input");
    var $subElement = $parent.find(".js-number-sub");
    var iptValue = parseInt($ipt.val());

    if(!isNaN(iptValue)){
        iptValue++;
        $ipt.val(iptValue);
        $subElement.removeClass("mallui_number-disabled");
    }
}

//操作数量输入框
function OptionIpt(input){
    var $subElement = input.prev(".js-number-sub");
    var $parent = input.closest(".js-numberbox");
    var iptValue = parseInt(input.val());

    if(!isNaN(iptValue)){
        if(iptValue < 1) {
            input.val(1);
            $subElement.addClass("mallui_number-disabled");
        }
        else if(iptValue == 1){
            $subElement.addClass("mallui_number-disabled");
        }
        else{
            $subElement.removeClass("mallui_number-disabled");
        }
    }
}

//完成按钮
function CompleteSelect(){
    var $goodsPopup = $("#goodsPopup");
    var $propertyItem = $goodsPopup.find(".js-property-item");
    var $selectedName =  $("#selectedName");
    var $tapId = $("#tapId");
    var tapId = parseInt($tapId.val());
    var name = $goodsPopup.find(".js-item-name").text();
	

    // console.log(tapId);
    if(!$propertyItem.hasClass("on")){
        return ;
    }
    else{
        switch(tapId){
            case 0:
                $selectedName.text(name);
                $(".js-join-cart").removeClass("open-popup");
                $(".js-buy-btn").removeClass("open-popup");
                break;
            case 1:
                $selectedName.text(name);
                intoCart();
                break;
            case 2:
                orderConfirm();
                break;
        }
    }
}

//加入购物车
function intoCart(){
	var $selected = $(".js-property-item.on");
    var count = parseInt($(".js-number-input").val());

    if(!isNaN(count)){
        $.ajax({
            type: "post",
            url: "intoCart",
            dataType:'json',
            data: {
                goodsId : $("#goodsId").val(),
                sellerId : $selected.attr("data-sellerid"),
                goodsItemId : $selected.attr("id"),
                count : count,
                retPg : "items"
            },
            success: function(data) {
                if(data.returnFlag == 1){
                    location.href = "../user/login?returnPage=" + data.returnPage;
                }
                else if(data.returnFlag == 2){
                    $.alert(data.rtnMsg);
                }
                else{
                    $.closePopup();
                    $.toast("加入购物车成功");
                    $(".js-join-cart").removeClass("open-popup");
                }
            },
            error: function(errorMsg) {
                $.alert("请稍后加入购物车!");
            }
        });
    }
    else{
        $.alert("请输入商品数量");
    }
}

function orderConfirm(){
	var id = $(".js-property-item.on").attr("id");
	var count = parseInt($(".js-number-input").val());
	location.href = "../order/orderConfirm?goodsItemIds=" + id + "&counts=" + count;
}
